package com.huaruan.youdi.utils;

import android.os.CountDownTimer;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

public class TimeUtils {

    public static String timeFormat(long timeMillis, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.CHINA);
        return format.format(new Date(timeMillis));
    }

    public static String formatPhotoDate(long time) {
        return timeFormat(time, "yyyy-MM-dd");
    }

    public static String formatPhotoMonth(long time) {
        return timeFormat(time, "yyyy年MM月");
    }

    public static String formatPhotoDate(String path) {
        File file = new File(path);
        if (file.exists()) {
            long time = file.lastModified();
            return formatPhotoDate(time);
        }
        return "1970-01-01";
    }

    public static long getCurrentTime() {
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        long time = cal.getTimeInMillis();
        return time;
    }

    public static String getDateForMMMTime(long time) {
        //注意传入的要是一个毫秒
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("MMM", Locale.ENGLISH);
        return sf.format(d);
    }

    public static String getDateForDayTime(long time) {
        //注意传入的要是一个毫秒
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("d", Locale.ENGLISH);
        return sf.format(d);
    }

    public static long local2UTC(int year, int month, int day, int hour, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new GregorianCalendar(year, month, day, hour, minute).getTime());

        return calendar.getTimeInMillis();
    }

    public static String utc2Local(long utcDateTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date(utcDateTime));
    }


    /**
     * 时间差
     *
     * @param time
     * @return
     */
    public static String getTimeFormatText(long time) {
        long minute = 60 * 1000;// 1分钟
        long hour = 60 * minute;// 1小时
        long day = 24 * hour;// 1天
        long month = 31 * day;// 月
        long year = 12 * month;// 年

        Date date = new Date(time);

        if (date == null) {
            return null;
        }
        long diff = new Date().getTime() - date.getTime();
        long r = 0;
        if (diff > year) {
            r = (diff / year);
            return r + "年前";
        }
        if (diff > month) {
            r = (diff / month);
            return r + "个月前";
        }
        if (diff > day) {
            r = (diff / day);
            return r + "天前";
        }
        if (diff > hour) {
            r = (diff / hour);
            return r + "小时前";
        }
        if (diff > minute) {
            r = (diff / minute);
            return r + "分钟前";
        }
        return "刚刚";
    }

    public static String getTime(long time) {

        long day = time / (1000 * 24 * 60 * 60); //单位天

        long hour = (time - day * (1000 * 24 * 60 * 60)) / (1000 * 60 * 60);
        //单位时
        long minute = (time - day * (1000 * 24 * 60 * 60) - hour * (1000 * 60 * 60)) / (1000 * 60);
        //单位分
        long second = (time - day * (1000 * 24 * 60 * 60) - hour * (1000 * 60 * 60) - minute * (1000 * 60)) / 1000;
        //单位秒
        String dayString = day + "";
        String hourString = hour + "";
        String minuteString = minute + "";
        String secondString = second + "";
        if (!"0".equals(dayString)) {
            return dayString + "天" + hourString + "时" + minuteString + "分" + secondString + "秒";
        } else if (!"0".equals(hourString)) {
            return hourString + "时" + minuteString + "分" + secondString + "秒";
        } else if (!"0".equals(minuteString)) {
            return minuteString + "分" + secondString + "秒";
        }
        return secondString + "秒";
    }

}